package cn.zhangxin.pethome.system.shop.controller;

import cn.zhangxin.pethome.constants.BaseConstants;
import cn.zhangxin.pethome.system.shop.domain.Shop;
import cn.zhangxin.pethome.system.shop.service.IShopAuditLogService;
import cn.zhangxin.pethome.system.shop.domain.ShopAuditLog;
import cn.zhangxin.pethome.system.shop.query.ShopAuditLogQuery;
import cn.zhangxin.pethome.utils.AjaxResult;
import cn.zhangxin.pethome.utils.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.prefs.BackingStoreException;

@RestController
@RequestMapping("/shopAuditLog")
@Api(value = "商家入驻状态日志的API",description="商家入驻状态日志功能")
public class ShopAuditLogController {
    @Autowired
    public IShopAuditLogService shopAuditLogService;

    /**
     * 保存和修改公用的
     *
     * @param shopAuditLog 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ShopAuditLog shopAuditLog, HttpServletRequest request) {
        try {
            if (shopAuditLog.getId() != null)
                shopAuditLogService.update(shopAuditLog);
            else
                shopAuditLogService.add(shopAuditLog,BaseConstants.Shop.STATE_ACTIVE_SUCCESS,request);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    @PostMapping("/{ShopId}")
    @ApiOperation(value = "店铺邮件点击确认接口" )
    public void storeAffirm(@PathVariable Long ShopId, HttpServletRequest request) {
        ShopAuditLog shopAuditLog = new ShopAuditLog();
        shopAuditLog.setShopId(ShopId);
        shopAuditLog.setNote("激活成功");
        shopAuditLogService.add(shopAuditLog,BaseConstants.Shop.STATE_ACTIVE_SUCCESS,request);
    }
    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopAuditLogService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public ShopAuditLog get(@PathVariable("id") Long id) {
        return shopAuditLogService.findOne(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<ShopAuditLog> list() {

        return shopAuditLogService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody ShopAuditLogQuery query) {
        try {
            List<ShopAuditLog> byQuery = shopAuditLogService.findByQuery(query);
            Long total = shopAuditLogService.findByQueryCount(query);
            System.out.println(total);
            return AjaxResult.success().setData(byQuery).setTotal(total);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @PostMapping("/audit/reject")
    @ApiOperation(value = "驳回接口" )
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog, HttpServletRequest request){

        try {
            shopAuditLogService.add(shopAuditLog,BaseConstants.Shop.STATE_REJECT_AUDIT,request);
            return AjaxResult.success().setMsg("驳回成功");
        }catch (BusinessException businessException){
            return AjaxResult.error().setMsg(businessException.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("驳回失败,请联系管理员");
    }


}